<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/7/2 0002
 * Time: 16:27
 */

namespace Wx\Controller;


class WholesaleOrderController extends CommonController
{
    //订单列表
    public function index(){
        $type = $_REQUEST['type']?$_REQUEST['type']:0;
        $u_id = session("u_id");
        switch ($type){
            case 0:
                $where = " AND state in(2,3,4,5,6)";
                break;
            case 3:
                $where = " AND state = 3";
                break;
            case 2:
                $where = " AND state = 2";
                break;
            case 4:
                $where = " AND state = 4";
                break;
            case 5:
                $where = " AND state = 5";
                break;
            case 6:
                $where = " AND state = 6";
                break;
        }//end switch

        $wholesale = M("wholesale")->where("u_id = {$u_id}".$where)->order('addtime desc')->select();//订单信息

        foreach($wholesale as $wk => $wv){
            // 商品批发区间
            $wholesale[$wk]['price_range']=M('spellgroup_price_range')->where('sg_id='.$wv['g_id']." AND price = {$wv['price']}")->field("start,end")->find();
            $wholesale[$wk]['store']=M("shop_spellgroup")->where("id = {$wv['g_id']}")->field("id,title,price,h_imgs,u_num,unit")->find();//获取商品信息
            //发货人信息
            $wholesale[$wk]['member'] = getMember($wv['g_u_id'],'avatar,nickname');//获取头像姓名
            $wholesale[$wk]['price_all'] = ($wv['price']*$wv['num']);//商品总价
            $wholesale[$wk]['price_charge'] = $wholesale[$wk]['price_all']*3/100;//服务费
            $wholesale[$wk]['village'] = M("member_farmers")->where("u_id = {$wv['g_u_id']}")->getField("village");//获取地址
        }//end foreach

        $this->assign("type",$type);
        $this->assign("wholesale",$wholesale);
        $this->display("order-pf");

//        dump($wholesale);
    }//end fun index()

    //订单详情
    public function detail(){
        $id = $_GET['id'];//订单id
        $wholesale = M("wholesale")->where("id = {$id}")->find();//订单信息


        // 商品批发区间
        $wholesale['price_range']=M('spellgroup_price_range')->where('sg_id='.$wholesale['g_id']." AND price = {$wholesale['price']}")->field("start,end")->find();
        $wholesale['store']=M("shop_spellgroup")->where("id = {$wholesale['g_id']}")->field("id,title,price,h_imgs,u_num,unit")->find();//获取商品信息
        //发货人信息
        $wholesale['member'] = getMember($wholesale['g_u_id'],'avatar,nickname');//获取头像姓名
        $wholesale['price_all'] = ($wholesale['price']*$wholesale['num']);//商品总价
        $wholesale['price_charge'] = $wholesale['price_all']*3/100;//服务费

        $wholesale['village'] = M("member_farmers")->where("u_id = {$wholesale['g_u_id']}")->getField("village");//获取地址


        $this->assign("wholesale",$wholesale);

        if($wholesale['state']==6){
            $evaluate = M("wholesale_evaluate")->where("u_id = {$wholesale['u_id']} AND g_id = {$wholesale['g_id']} AND w_id = {$wholesale['id']} AND state = 1")->find();//评论信息
            $evaluate['pic'] = explode(',',$evaluate['pic']);//图片处理
            $this->assign("evaluate",$evaluate);
            $this->display("myevaluation");//评论页
        }else{
            $this->display("orderdetail-pf");//详情页
        }//end if
//                dump($wholesale);

    }//end fun

    //订单支付
    public function pay(){
        $id = $_POST['id'];//订单id
        $info = M("wholesale")->where("id = {$id} AND state = 3")->find();//订单信息
        if($info){
            $time = M("wholesale_pay_log")->where("id = {$info['pay_log_id']}")->getField('addtime');
            $price_all = $info['price']*$info['num'];//总价
            $price_all += $price_all*3/100;//加手续费
            $pay_log = array(
                'u_id' => $info['u_id'],
                'total_fee' => $price_all,
                'order_sn' => $info['order_sn'],
                'addtime' => $time
            );
            $addpay = M("wholesale_pay_log")->add($pay_log);
            if($addpay){
                M("wholesale")->where("id = {$id} AND state = 3")->save(['pay_log_id'=>$addpay]);
                $this->ajaxReturn(['state'=>0,'id'=>$addpay],"JSON");
            }else{
                $this->ajaxReturn(['state'=>1,'val'=>'支付失败'],"JSON");
            }//end if
        }else{
            $this->ajaxReturn(['state'=>1,'val'=>'参数错误'],"JSON");
        }//end if

    }//end fun

    //提醒发货
    public function deliver(){
        $id = $_POST['id'];//订单id
        $info = M("wholesale")->where("id = {$id}")->find();//订单信息
        $res = sendVoice($info['g_u_id'],"你好，订单编号：{$info['order_sn']}的货主，提醒你发货！");
        if($res){
            $this->ajaxReturn(['state'=>0,'val'=>'已提醒发货'],"JSON");
        }else{
            $this->ajaxReturn(['state'=>1,'val'=>'发货提醒失败'],"JSON");
        }//end if
    }//end fun

    //取消订单
    public function delete(){
        $id = $_POST['id'];//订单id
        $info = M("wholesale")->where("id = {$id}")->field('num,g_id')->find();//订单信息
        $addnum = M("shop_spellgroup")->where("id = {$info['g_id']}")->setInc('num',$info['num']);//退还库存
        if($addnum){
            M("wholesale")->where("id = {$id}")->delete();//删除订单
            $this->ajaxReturn(['state'=>0],"JSON");
        }else{
            $this->ajaxReturn(['state'=>1],"JSON");
        }//end if
    }//end fun

    //确认收货
    public function take(){
        $id = $_POST['id'];//订单id
        $info = M("wholesale")->where("id = {$id}")->save(['state'=>5]);//订单信息
        if($info){
            $this->ajaxReturn(['state'=>0],"JSON");
        }else{
            $this->ajaxReturn(['state'=>1,'val'=>'参数错误'],"JSON");
        }//end if
    }//end fun

    // 添加评价
    public function addEva(){
        if(IS_POST){
            $post = I('post.');
            $post['u_id'] = session('u_id');    // 用户id
            if($post['img_1']!=''||$post['img_2']!=''||$post['img_3']!=''){//如果有图片便进行处理
                $post['pic'] = $post['img_1'].$post['img_2'].$post['img_3'];    //拼接图片路径
                $post['pic'] = substr($post['pic'],0,strlen($post['pic'])-1);   //去掉路径最后的英文逗号
            }// end if
            $post['addtime'] = time();
            $res = M('wholesale_evaluate')->add($post);
            if($res) {
                // 更新订单状态-已评价
                M('wholesale')->where('id = '.$post['w_id'])->setField('state',6);
                $this->ajaxReturn(['state'=>0,'val'=>'评论成功'],"JSON");
            }else{
                $this->ajaxReturn(['state'=>1,'val'=>'评论失败'],"JSON");
//                $this->error('添加失败，请检查');
            }// end if
        }else{
            $w_id = intval(I('wid'));  //订单id
            $order = M("wholesale")->where("id = {$w_id}")->getField('g_id');
            $g_id = intval($order);  // 商品id
            $this -> assign('w_id',$w_id);
            $this -> assign('g_id',$g_id);
            $this->display('evaluate');
        }// end if
    }// end fun addEva()

    //物流查看
    public function logistics(){
        $this->error("功能尚未开发");
    }//end fun

}//end class